package cn.jishiyu11.xeqb.activity.personalCenterActivitys

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import cn.jishiyu11.xeqb.EntityClass.FragmengData.MsgData
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.adapter.MSGadapter
import cn.jishiyu11.xeqb.base.BaseActivity
import cn.jishiyu11.xeqb.http.http.i.DataCallBack
import cn.jishiyu11.xeqb.http.http.i.httpbase.MSG_CENTER
import cn.jishiyu11.xeqb.http.http.i.httpbase.OkHttpManager
import cn.jishiyu11.xeqb.utils.USER_UserNo
import cn.jishiyu11.xeqb.utils.showShortToast
import com.google.gson.Gson
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.act_messageactivity_layout.*
import kotlinx.android.synthetic.main.footermore.*
import kotlinx.android.synthetic.main.frg_loancard_layout.*
import kotlinx.android.synthetic.main.tab_activity_fra.*
import okhttp3.Request
import java.io.IOException

/**
 * Created by jsy_zj on 2018-03-06.
 * 消息中心
 */
class MessageCenter : BaseActivity(), DataCallBack {
    var DiscList: MutableList<MsgData.Data>? = null
    var mAdapter: MSGadapter? = null
    var discBean: MsgData? = null
    var mIsRefresh = false
    var page = 1
    var mIsAddMore = false
    var FilderName = "MSG_URL"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_messageactivity_layout)
        initView()
    }

    override fun findViewById() {}

    override fun initView() {
        title_image.visibility = View.VISIBLE
        title_complete.visibility = View.GONE
        title_view.text = getString(R.string.personalcenter_message_center)
        title_image.setOnClickListener { finish() }
        act_messageactivity_layout_recycler.layoutManager = LinearLayoutManager(this)
        DiscList = ArrayList<MsgData.Data>()
        mAdapter = MSGadapter(this!!, DiscList!!)
        act_messageactivity_layout_recycler.adapter = mAdapter
        act_messageactivity_layout_recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                var layoutManager: LinearLayoutManager = recyclerView?.layoutManager as LinearLayoutManager
                var lastPosition = layoutManager.findLastVisibleItemPosition()
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastPosition == DiscList!!.size) {

                    if (!mIsAddMore) {
                        mIsAddMore = true
                        if (DiscList?.size != 0) {
                            page += 1
                            getData()
                        }

                    }
                }
            }
        })
        act_messageactivity_layout_srf.setOnRefreshListener {
            if (!mIsRefresh) {
                mIsRefresh = true
                page = 1
                getData()
            }
        }
        getData()
    }


    /**
     * recycler 数据刷新
     * */
    private fun setRecyclerData(result: String) {
        if (!result.isNullOrEmpty()) {
            if (mIsRefresh) {
                mIsRefresh = false
                DiscList?.clear()
                act_messageactivity_layout_srf.isRefreshing = false
            }

            discBean = Gson().fromJson(result, MsgData::class.java)
            when (discBean?.code!!.toInt()) {
                200 -> {
                    if (mIsRefresh && DiscList?.size!=0){
                        DiscList?.clear()
                    }

                    var msgList = discBean?.data
                    if (msgList != null) {
                        DiscList!!.addAll(msgList!!)
                    }
                    Refresh()
                    if (mIsAddMore) mIsAddMore = false
                    mAdapter?.notifyDataSetChanged()
                    footer_layout_text.setText(getString(R.string.load_more_pleace_wait))
                    footer_layout_loading.visibility = View.VISIBLE
                }
                205 -> {
                    AddMore()
                    Refresh()
                }
                300 -> {
                    AddMore()
                    Refresh()

                    showShortToast("系统关闭中！")
                }
            }
        }


    }


    /**
     * 获取数据
     * */
    private fun getData() {
//        OkHttpManager.postAsync(MSG_CENTER + "userID=2&page=$page", FilderName, null, this)
        OkHttpManager.postAsync(MSG_CENTER+"userID=$USER_UserNo&page=$page",FilderName,null,this)
    }

    override fun requestFailure(request: Request, name: String, e: IOException) {
        if (name.equals(FilderName)){
            if (mIsAddMore){
                mIsAddMore = false
                if (page > 1) page -= 1
                footer_layout_text.setText(getString(R.string.load_more_no_more))
                footer_layout_loading.visibility = View.GONE
            }
            if (mIsRefresh){
                mIsRefresh = false
                act_messageactivity_layout_srf.isRefreshing = false
            }

            this.showShortToast("数据获取失败！")

        }

    }

    private fun AddMore(){
        if (mIsAddMore){
            mIsAddMore = false
            if (page>1) page -=1
            footer_layout_text.setText(getString(R.string.load_more_no_more))
            footer_layout_loading.visibility = View.GONE
        }
    }

    private fun Refresh(){
        if (mIsRefresh) {
            mIsRefresh = false
            frg_home_refreshLayout.isRefreshing = false
        }
    }


    override fun requestSuccess(result: String, name: String) {
        result.trim()
        when (name) {
            FilderName -> {
                setRecyclerData(result)
            }
        }
    }


    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }


}
